/**
 * *******************************************
 * user: zmq
 * describe:    配置文件加载到内存中
 * created:
 * date: 2021/8/19
 * *******************************************
 */

import * as path from "path";

let configPath:string = `${__dirname}/../../config`;
let datas: any = {};

// 配置文件api类
export default class DataApi {

    /**
     * 数据直接读取，不做特殊操作，可使用这个函数来统一加载
     * @param childPath
     * @param name
     * @return {*}
     */
    private static directLoad(childPath, name) {
        if (datas[name]) {
            return datas[name];
        }
        let filePath = path.resolve(configPath + childPath, `${name}.json`);
        datas[name] = require(filePath);

        return datas[name];
    }

    /**
     * 服务端错误码配置
     * @param errCodeStr
     * @returns {*}
     */
    public static errCode(errCodeStr: string) {

        if (this.directLoad('/gameData/', 'errCodeServer')[errCodeStr]) {
            return this.directLoad('/gameData/', 'errCodeServer')[errCodeStr].errCode;
        }
        return this.directLoad('/gameData/', 'errCodeServer').PLEASE_UPDATE_ERR_CODE_CFG.errCode;
    }


    /**
     * 常量配置
     * @param type
     * @param {boolean} isDesc
     * @returns {any}
     */
    public static constVal(type: string, isDesc = false) {
        if (isDesc) {
            return this.directLoad('/gameData/', 'constVal')[type].desc;
        }
        return this.directLoad('/gameData/', 'constVal')[type].value;
    }
}
